package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.cache.redis.service.IStrategyStatisVOService;
import cn.wolfcode.wolf2w.cache.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.interceptor.RequiredLogin;
import cn.wolfcode.wolf2w.interceptor.UserParam;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import io.lettuce.core.dynamic.annotation.Param;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVOService strategyStatisVOService;

    @GetMapping("themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }


    @GetMapping("query")
    public Object query(StrategyQueryObject qo){
        Page<Strategy> strategies = strategyService.queryByThemeId(qo);
        return JsonResult.success(strategies);
    }

    @GetMapping("detail")
    public Object detail(String id){
        // 先获取是统计哪篇攻略的阅读量
        Strategy strategy = strategyService.get(id);
        // 通过自定义方法,让每次刷新阅读量+1
        strategyStatisVOService.viewNumStatis(id , 1);
        return JsonResult.success(strategy);


    }

    @RequiredLogin
    @PostMapping("addComment")
    public Object addComment(StrategyComment comment , @UserParam UserInfo userInfo){
        // 获取用户对象id
        BeanUtils.copyProperties(userInfo , comment);
        comment.setUserId(userInfo.getId());

        strategyCommentService.save(comment);
       //通过自定义方法,让每次发表评论后,评论数+1
        strategyStatisVOService.replyStatis(comment.getStrategyId() , 1);

        return JsonResult.success();
    }

    @GetMapping("comments")
    public Object comments(StrategyCommentQueryObject qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    @RequiredLogin
    @PostMapping("commentThumb")
    public Object commentThumb(String cid ,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid ,userInfo.getId());
        return JsonResult.success();
    }

    @RequiredLogin
    @GetMapping("statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVOService.getStrategyStatisVo(sid));
    }

    @RequiredLogin
    @PostMapping("favor")
    public Object favor(String sid , @UserParam UserInfo userInfo){
        boolean ref = strategyStatisVOService.favornumStatis(sid , userInfo.getId());
        return JsonResult.success(ref);
    }

    @RequiredLogin
    @PostMapping("strategyThumbup")
    public Object strategyThumbup(String sid , @UserParam UserInfo userInfo){
        boolean ret = strategyStatisVOService.strategyThumbup(sid , userInfo.getId());
        return JsonResult.success(ret);
    }
}
